﻿using CommunityToolkit.Mvvm.Messaging;
using DataViews.Controls;
using DataViews.Messages;
using DataViews.Models;
using DataViews.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataViews.Views
{
    /// <summary>
    /// DataListView.xaml 的交互逻辑
    /// </summary>
    public partial class DataListView : UserControl
    {
        public DataListView()
        {
            InitializeComponent();
            data_listview.SelectionChanged += (o, e) => { 
                WeakReferenceMessenger.Default.Send<SelectionChangedMessage>(new SelectionChangedMessage((this.DataContext as IContext)!.Context,data_listview.SelectedItems));
            };
            WeakReferenceMessenger.Default.Register<SelectAllMessage>(this, (r, e) =>
            {
                if(e.Context!=(this.DataContext as IContext)!.Context)
                {
                    return;
                }

                //
                data_listview.SelectedItems.Clear();

                if (e.IsChecked)
                {
                    foreach (var item in (e.ObjectContext as IEnumerable)!)
                    {
                        data_listview.SelectedItems.Add(item);
                    }
                }

            });
        }

        private void Data_listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


            
        }

        public void SetViewModelAndGenFileds<T>(DataListViewModel<T> vm)
        {

            //设定DataContext
            this.DataContext = vm;
            //

            GridViewColumn column;

            //增加checkbox列
            column = new GridViewColumn();
            var header = new GridViewColumnHeader();
            header.Content = new AllSelectHeaderControl ();
            header.Click += (sender, e) =>
            {                
                vm.AllItemsSelected = !vm.AllItemsSelected;
            };
            column.Header = header;
            column.Width = 100;
            column.CellTemplate = data_listview.Resources["checkbox"] as DataTemplate;

            data_gridview.Columns.Add(column);

            int i = 0;
            foreach (var item in vm.DisplayFields)
            {
                var field_info = typeof(T).GetProperty(item.FieldName!);
                var field_type = field_info!.PropertyType;
                //
                column = new GridViewColumn();

                //header
                var field_header = new GridViewColumnHeader();
                var sorted_header = new SortedHeaderControl();
                sorted_header.field_name.Content = item.FieldDisplayName;
                field_header.Content = sorted_header;
                field_header.Click += (sender, args) =>
                {
                    WeakReferenceMessenger.Default.Send<SortedHeaderClickMessage>(new SortedHeaderClickMessage((this.DataContext as IContext)!.Context, item));
                };
                column.Header = field_header;
                sorted_header.order_by.SetBinding(Label.ContentProperty, new Binding($"DisplayFields[{i++}].OrderBy"));

                //
                column.Width = item.Width;
                // 创建CellTemplate
                FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBox));

                var binding = new Binding($"{item.FieldName}");
                if (field_type == typeof(DateOnly) || field_type == typeof(DateOnly?))
                {
                    binding.StringFormat = "yyyy-MM-dd";
                }
                else if (field_type == typeof(DateTime) || field_type == typeof(DateTime?))
                {
                    binding.StringFormat = "yyyy-MM-dd hh:mm:ss";
                }

                factory.SetBinding(TextBox.TextProperty, binding);
                factory.SetValue(TextBox.IsReadOnlyProperty, true);
                factory.SetValue(TextBox.BorderThicknessProperty, new Thickness(0));


                DataTemplate dataTemplate = new DataTemplate();
                dataTemplate.VisualTree = factory;
                column.CellTemplate = dataTemplate;



                data_gridview.Columns.Add(column);
            }


        }


        
    }
}
